<?php

namespace application\system\controller;

/**
 * Description of AuthController
 *
 * @author Faraj
 */
class AuthController extends \simbola\core\application\AppController {

    public function __construct() {
        $this->securityBypass = array('session');
    }
    
    public function actionIndex() {
        $this->view('auth/index');
    }
    
    public function actionSocial() {
        \Hybrid_Endpoint::process($_REQUEST);
    }

    public function actionLogin() {
        if ($this->issetPost(array('username', 'password'))) {
            try {
                $this->invoke("system", "auth", "login", array(
                    'username' => $this->post('username'),
                    'password' => $this->post('password'),
                ));
                $this->redirect(\simbola\Simbola::app()->router->getParam('DEFAULT'));
            } catch (\Exception $ex) {
                $this->setViewData("errorMessage", $ex->getMessage());
            }
        }
        $this->view('auth/login');
    }

    public function actionLogout() {
        $this->invoke("system", "auth", "logout", array());
        $this->redirect(\simbola\Simbola::app()->router->getParam('DEFAULT'));
    }
    
    // User --------------------------------------------------------------------
    
       
    function actionUser(){        
        try {
            $response = $this->invoke('system', 'auth', 'userList', array(
                'search' => $this->post('data'),
            ));
            $data = $response["body"]['response']['data'];
            $this->setViewData('data', $data);
        } catch (\Exception $ex) {
            $this->setViewData("error", $ex->getMessage());
        }        
        $this->view('auth/user/index');
    }
    
    function actionUserCreate() {
        $object = new \application\system\model\auth\User();
        if ($this->issetPost('data')) {
            try {
                $response = $this->invoke('system', 'auth', 'userCreate', array(
                    'data' => $this->post('data'),
                ));
                $object = $response["body"]['response']['object'];
                $keys = array(
                    "user_id" => $object->user_id
                );
                $this->redirect('/system/auth/userView',$keys);
            } catch (\simbola\core\component\system\lib\exception\ServiceUserException $ex) {
                $object = $ex->getResponse('object');
                $this->setViewData("error", $object->errors->full_messages()); 
            } catch (\Exception $ex) {
                $this->setViewData("error", $ex->getMessage());
            }
        }
        $this->setViewData('object',$object);
        $this->view('auth/user/create');
    }
    
    function actionUserUpdate(){
        if ($this->issetGet('user_id')) {
            $keysFromGet = array(
                "user_id" => $this->get("user_id")
            );
            $object = $this->getUserObject($keysFromGet);            
            if(is_null($object)){
                $this->view('auth/user/notFound');
            }else{
                $this->setViewData('object',$object);
                if ($this->issetPost('data')) {
                    try {
                        $response = $this->invoke('system', 'auth', 'userUpdate', array(
                            'keys' => $keysFromGet,
                            'data' => $this->post('data'),
                        ));
                        $object = $response["body"]['response']['object'];                        
                        $this->setViewData('object',$object);
                        $keys = array(
                            "user_id" => $object->user_id
                        );
                        $this->redirect('/system/auth/userView',$keys);
                    } catch (\simbola\core\component\system\lib\exception\ServiceUserException $ex) {
                        $object = $ex->getResponse('object');
                        $this->setViewData("error", $object->errors->full_messages()); 
                        $this->setViewData('object', $object);
                    } catch (\Exception $ex) {
                        $this->setViewData("error", $ex->getMessage());
                    }
                }
                $this->view('auth/user/update');
            }
        }else{
            $this->redirect('/system/auth/user');
        }
    }
    
    function actionUserDelete(){
        if ($this->issetGet('user_id')) {
            if ($this->issetPost('keys')) {
                $keys = $this->post('keys');
                try {
                    $response = $this->invoke('system', 'auth', 'userDelete', array(
                        'keys' => $keys,                    
                    ));                                
                    $this->redirect('/system/auth/user');
                } catch (\Exception $ex) {
                    $this->setViewData("error", $ex->getMessage());
                    $this->setViewData('object', $this->getUserObject($keys));
                    $this->view('auth/user/delete');
                }
            } else {
                $keys = array(
                    "user_id" => $this->get("user_id")
                );
                $object = $this->getUserObject($keys);
                if(is_null($object)){
                    $this->view('auth/user/notFound');
                }else{
                    $this->setViewData('object', $object);
                    $this->view('auth/user/delete');
                }
            }   
        } else {
            $this->redirect('/system/auth/user');
        }
    }

    function actionUserView() {
        if ($this->issetGet('user_id')) {            
            $keys = array(
                "user_id" => $this->get("user_id")
            );
            $object = $this->getUserObject($keys);
            if(is_null($object)){
                $this->view('auth/user/notFound');
            }else{
                $this->setViewData('object', $object);
                $this->view('auth/user/view');
            }
        } else {
            $this->redirect('/system/auth/user');
        }
    }

    //private functions
    private function getUserObject($keys){        
        try{
            $response = $this->invoke('system', 'auth', 'userView', array(
                 'keys' => $keys,
            ));
            return $response["body"]['response']['object'];
        }catch(\Exception $ex){
            return null;
        }
    }
    
    // Role --------------------------------------------------------------------
    function actionRole() {
        try {
            $response = $this->invoke('system', 'auth', 'roleList', array(
                'search' => $this->post('data'),
            ));
            $data = $response["body"]['response']['data'];
            $this->setViewData('data', $data);
        } catch (\Exception $ex) {
            $this->setViewData("error", $ex->getMessage());
        }        
        $this->view('auth/role/index');
    }

    function actionRoleCreate() {
        $object = new \application\system\model\auth\Role();
        if ($this->issetPost('data')) {
            try {
                $response = $this->invoke('system', 'auth', 'roleCreate', array(
                    'data' => $this->post('data'),
                ));
                $object = $response["body"]['response']['object'];
                $keys = array(
                    "title" => $object->title
                );
                $this->redirect('/system/auth/roleView',$keys);
            } catch (\simbola\core\component\system\lib\exception\ServiceUserException $ex) {
                $object = $ex->getResponse('object');
                $this->setViewData("error", $object->errors->full_messages()); 
            } catch (\Exception $ex) {
                $this->setViewData("error", $ex->getMessage());
            }
        }
        $this->setViewData('object',$object);
        $this->view('auth/role/create');
    }
    
    function actionRoleUpdate(){
        if ($this->issetGet('title')) {
            $keysFromGet = array(
                "title" => $this->get("title")
            );
            $object = $this->getRoleObject($keysFromGet);            
            if(is_null($object)){
                $this->view('auth/role/notFound');
            }else{
                $this->setViewData('object',$object);
                if ($this->issetPost('data')) {
                    try {
                        $response = $this->invoke('system', 'auth', 'roleUpdate', array(
                            'keys' => $keysFromGet,
                            'data' => $this->post('data'),
                        ));
                        $object = $response["body"]['response']['object'];
                        $this->setViewData('object',$object);
                        $keys = array(
                            "title" => $object->title
                        );
                        $this->redirect('/system/auth/roleView',$keys);
                    } catch (\simbola\core\component\system\lib\exception\ServiceUserException $ex) {
                        $object = $ex->getResponse('object');
                        $this->setViewData("error", $object->errors->full_messages()); 
                        $this->setViewData('object', $object);
                    } catch (\Exception $ex) {
                        $this->setViewData("error", $ex->getMessage());
                    }
                }
                $this->view('auth/role/update');
            }
        }else{
            $this->redirect('/system/auth/role');
        }
    }
    
    function actionRoleDelete(){
        if ($this->issetGet('title')) {
            if ($this->issetPost('keys')) {
                $keys = $this->post('keys');
                try {
                    $response = $this->invoke('system', 'auth', 'roleDelete', array(
                        'keys' => $keys,                    
                    ));                                
                    $this->redirect('/system/auth/role');
                } catch (\Exception $ex) {
                    $this->setViewData("error", $ex->getMessage());
                    $this->setViewData('object', $this->getRoleObject($keys));
                    $this->view('auth/role/delete');
                }
            } else {
                $keys = array(
                    "title" => $this->get("title")
                );
                $object = $this->getRoleObject($keys);
                if(is_null($object)){
                    $this->view('auth/role/notFound');
                }else{
                    $this->setViewData('object', $object);
                    $this->view('auth/role/delete');
                }
            }   
        } else {
            $this->redirect('/system/auth/role');
        }
    }

    function actionRoleView() {
        if ($this->issetGet('title')) {            
            $keys = array(
                "title" => $this->get("title")
            );
            $object = $this->getRoleObject($keys);
            if(is_null($object)){
                $this->view('auth/role/notFound');
            }else{
                $this->setViewData('object', $object);
                $this->view('auth/role/view');
            }
        } else {
            $this->redirect('/system/auth/role');
        }
    }

    //private functions
    private function getRoleObject($keys){        
        try{
            $response = $this->invoke('system', 'auth', 'roleView', array(
                 'keys' => $keys,
            ));
            return $response["body"]['response']['object'];
        }catch(\Exception $ex){
            return null;
        }
    }

}

?>
